package service;

import mapper.ManagerMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.Manager;
import util.SqlSessionFactoryUtil;

import java.util.List;

public class ManagerService {
    private final SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    public boolean managerLogin(Manager manager) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);

        List<Manager> managers = mapper.selectOneOfManager(manager);

        sqlSession.close();
        return !managers.isEmpty();
    }

    public Manager selectOneManager(Manager manager) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);

        List<Manager> managers = mapper.selectOneManagerOfUserNo(manager.get_userNo());

        sqlSession.close();
        return managers.isEmpty() ? null : managers.get(0);
    }

    public void updateManagerInfo(Manager manager) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);

        mapper.updateManagerInfo(manager);

        sqlSession.commit();
        sqlSession.close();
    }
}
